#include "topbar.h"
#include <QtGui>
#include <QUrl>

TopBar::TopBar(QWidget *parent):
        QFrame(parent)
{
    style1 = "color:#124e65;margin-top:2px;margin-right:2px;font:bold 13px;font-family:sans-serif;";
    style2 = "color:#ffcf6e;margin-top:2px;margin-right:2px;font:bold 13px;font-family:sans-serif;";
    style3 = "color:#BF0000;margin-top:2px;margin-right:2px;font:bold 13px;font-family:sans-serif;";
    //Objects
    userName = new QLabel(this);
        userName->setTextFormat(Qt::PlainText);
    iconShadow = new QLabel(this);
    userIcon = new QLabel(iconShadow);
    statusEdit = new StatusEdit(this);
        statusEdit->setFixedHeight(DEFAULT_TEXTEDIT_HEIGHT);
    leftChars = new QLabel(this);
        leftChars->setTextFormat(Qt::RichText);
    wauDoing = new QLabel("What are you doing?...", this);
        wauDoing->setTextFormat(Qt::PlainText);

    //
    QGridLayout *iconLayout = new QGridLayout();
    iconLayout->addWidget(userIcon, 0, 0, Qt::AlignCenter);
    iconShadow->setLayout(iconLayout);

    //Object Names for qss
    userName->setObjectName("userName");
    userIcon->setObjectName("userIcon");
    statusEdit->setObjectName("statusEdit");
    leftChars->setObjectName("leftChars");
    wauDoing->setObjectName("wauDoing");
    iconShadow->setObjectName("iconShadow");

    //UserName and UserIcon at left side of widget
    QVBoxLayout *leftLayout = new QVBoxLayout();
    leftLayout->addWidget(userName, 0, Qt::AlignHCenter);
    leftLayout->addWidget(iconShadow, 0, Qt::AlignHCenter);
    leftLayout->addStretch(1);

    //wauDoing and leftChars
    QHBoxLayout *topLayout = new QHBoxLayout();
    topLayout->addWidget(wauDoing, 1);
    topLayout->addWidget(leftChars);

    //topLayout and statusEdit
    QVBoxLayout *rigthLayout = new QVBoxLayout();
    rigthLayout->addLayout(topLayout);
    rigthLayout->addWidget(statusEdit);
    rigthLayout->addStretch(1);

    //leftLayout and rigthLayout to make a main layout
    QHBoxLayout *mainLayout = new QHBoxLayout();
    mainLayout->addLayout(leftLayout);
    mainLayout->addLayout(rigthLayout);

    //Remove all spacing between widgets
    mainLayout->setMargin(0);
    mainLayout->setSpacing(0);
    topLayout->setSpacing(0);
    leftLayout->setSpacing(0);
    rigthLayout->setSpacing(0);

    setLayout(mainLayout);

    http = new QHttp(this);
    buffer = new QBuffer(this);

    //Size
    userIcon->setFixedSize(DEFAULT_ICON_SIZE, DEFAULT_ICON_SIZE);
    iconShadow->setFixedSize(DEFAULT_ICON_SHADOW_SIZE, DEFAULT_ICON_SHADOW_SIZE);

    //Disable ScrollBars
    statusEdit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    statusEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    connect(statusEdit, SIGNAL(textChanged()), this, SLOT(updateLeftChars()));
    connect(statusEdit, SIGNAL(keyPressed()), this, SLOT(keyPressed()));

    connect(http, SIGNAL(requestFinished(int,bool)), this, SLOT(requestFinished(int,bool)));

    //init
    updateLeftChars();
    changeIcon();
    changeIconBorder();
}

void TopBar::changeIcon(const QString fileName)
{
    userIcon->setPixmap(QPixmap(fileName));
}

void TopBar::changeIconBorder(TopBar::IconBorder ib)
{
    QString iconMask = 0;
    switch ( ib )
    {
        case TopBar::AllRounded:
            iconMask = DEFAULT_ICON_MASK0;
            break;
        case TopBar::LTRBRounded:
            iconMask = DEFAULT_ICON_MASK1;
            break;
        case TopBar::LBRTRounded:
            iconMask = DEFAULT_ICON_MASK2;
            break;
        default:
            break;
    }
    iconMask == 0 ? userIcon->setMask(QBitmap()) : userIcon->setMask(QBitmap(iconMask));
}

void TopBar::changeName(const QString name)
{
    userName->setText(name);
}

void TopBar::changeText(const QString text)
{
    statusEdit->setText(text);
    statusEdit->moveCursor(QTextCursor::End);
    statusEdit->setFocus();
}

void TopBar::clearText()
{
    statusEdit->clear();
}

void TopBar::changeWauDoing(const QString text)
{
    wauDoing->setText(text);
}

void TopBar::updateLeftChars()
{
    QString text = statusEdit->toPlainText();
    int chars = DEFAULT_CHARS - text.length();
    leftChars->setNum(chars);
    if(chars >30)
    {
        leftChars->setStyleSheet(style1);
    }
    else if(chars > 0 && chars <= 30)
    {
        leftChars->setStyleSheet(style2);
    }
    else
    {
        leftChars->setStyleSheet(style3);
    }
}

//Get image
//==================================================================================================
void TopBar::downloadIcon(const QString url)
{
    QUrl _url = QUrl(url);
    buffer->open(QIODevice::ReadWrite);

    http->setHost(_url.host());
    http_id = http->get(_url.path(), buffer);
}

void TopBar::requestFinished(int id, bool error)
{
    if(error) return;
    if(http_id == id && http->lastResponse().statusCode() == 200)
    {
        QPixmap image;
        QByteArray imagedata = buffer->data();
        if(image.loadFromData(imagedata)) userIcon->setPixmap(image);
        buffer->close();
    }
}
//==================================================================================================



void TopBar::enableStatusEdit(bool enable)
{
    enable ? statusEdit->setEnabled(true) : statusEdit->setEnabled(false);
}

void TopBar::keyPressed()
{
    statusEdit->setEnabled(false);
    QString status = statusEdit->toPlainText().simplified();
    emit updateStatus(status);
}

TopBar::~TopBar()
{

}
